package com.nannybear.Communication;

import android.util.Log;

import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.ExecutionException;

/**
 * \brief Send message to NannyBear
 * \author Audrey LOISEAU
 * \date 30-03-2015
 */

/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class ProxyRobot implements Observer{
    /********** DECLARATIONS **********/
    private final Connection connection;

    /************ METHODS ************/
    /**
     * \brief Constructor
     * \param connection    The connection
     */
    public ProxyRobot(Connection connection){
        this.connection = connection;
    }

    /**
     * \brief Send a asynchronous message
     * \param message   The message to send
     */
    private void sendMessage(String message){
        if(connection.isConnected()) {
            EmissionTask emissionTask = new EmissionTask(this.connection);
            try {
                emissionTask.execute(message).get();
            } catch (ExecutionException ex) {
                Log.e("ProxyRobot", "Interrupted task : " + ex.getCause());
            } catch (InterruptedException ex) {
                Log.e("ProxyRobot", "Aborted task : " + ex.getMessage());
            }
        }
    }

    /**
     * \brief Send the light threshold (between dark and dim)
     * \param lightThresholdDarkDim The value of light
     */
    private void sendLightThresholdDarkDim(int lightThresholdDarkDim){
        String message = String.valueOf(Protocol.LIGHT_THRESHOLD_DARK_DIM)+"/"+String.valueOf(lightThresholdDarkDim)+"-";
        sendMessage(message);
    }

    /**
     * \brief Send the light threshold (between dim and bright)
     * \param lightThresholdDimBright The value of light
     */
    private void sendLightThresholdDimBright(int lightThresholdDimBright){
        String message = String.valueOf(Protocol.LIGHT_THRESHOLD_DIM_BRIGHT)+"/"+String.valueOf(lightThresholdDimBright)+"-";
        sendMessage(message);
    }

    /**
     * \brief Send the sound threshold
     * \param soundThreshold The value of sound
     */
    private void sendSoundThreshold(int soundThreshold){
        String message = String.valueOf(Protocol.SOUND_THRESHOLD)+"/"+String.valueOf(soundThreshold)+"-";
        sendMessage(message);
    }

    /**
     * \brief Send a ack to know if the connection is lost or not
     */
    public void sendAck(){
        sendMessage("~C");
    }

    /**
     * \brief Ask a reset for the average temperature
     */
    public void sendResetTemperature(){
        String message = String.valueOf(Protocol.RESET_TEMP)+"/0-";
        sendMessage(message);
    }

    /**
     * \brief Ask a reset for the average humidity
     */
    public void sendResetHumidity(){
        String message = String.valueOf(Protocol.RESET_HUM)+"/0-";
        sendMessage(message);
    }

    /**
     * \brief Send the state of the night light
     * \param state off or color of the night light
     */
    public void sendStateNightLight(int state){
        String message = String.valueOf(Protocol.NIGHT_LIGHT_STATE)+"/"+state+"-";
        sendMessage(message);
    }

    /**
     * \brief Send the selected led
     * \param selectedLed   The led selected, their color and the time
     */
    public void sendSelectedLed(String selectedLed){
        String message = String.valueOf(Protocol.SELECTED_LED)+"/"+selectedLed+"-";
        sendMessage(message);
    }

    /**
     * \brief Send the animation wanted
     * \param animation The animation number
     */
    public void sendAnimation(int animation){
        String message = String.valueOf(Protocol.LED_ANIMATION)+"/"+animation+"-";
        sendMessage(message);
    }

    /**
     * \brief Start the lullaby
     */
    public void startLullaby(){
        String message = String.valueOf(Protocol.LULLABY+"/0-");
        sendMessage(message);
    }

    /**
     * \brief Update information with new value of thresholds
     * \param obs   The observable class
     * \param obj   The object received
     */
    public void update(Observable obs, Object obj){
        String name = obs.getClass().getSimpleName();
        if(name.equalsIgnoreCase("SoundThreshold")){
            sendSoundThreshold(Integer.parseInt(obj.toString()));
        }
        if(name.equalsIgnoreCase("LightThreshold")){
            String storage[] = obj.toString().split("/");
            sendLightThresholdDarkDim(Integer.parseInt(storage[0]));
            sendLightThresholdDimBright(Integer.parseInt(storage[1]));
        }
    }
}
